﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CFSystem.JXC.BasisInfo
{
    using CFSystem.JXC.BLL;
    using CFSystem.JXC.Model;

    public partial class FrmProductsMaintenance : Form
    {
        public FrmProductsMaintenance()
        {
            InitializeComponent();

            LoadAllProducts();
        }

        private void LoadAllProducts()
        {
            ProductBLL productBLL = new ProductBLL();
            IList<ProductInfo> products = productBLL.GetAllProducts();
            dgvProducts.DataSource = products;
        }

        private void AddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmProductInfo frmProdInfo = new FrmProductInfo();
            frmProdInfo.UpdateDataGridView += new FrmProductInfo.UpdateDataBinding(LoadAllProducts);
            frmProdInfo.ShowDialog();
        }

        private void ModifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmProductInfo frmProdInfo = new FrmProductInfo(dgvProducts.SelectedRows[0].Cells[0].Value.ToString());
            frmProdInfo.UpdateDataGridView += new FrmProductInfo.UpdateDataBinding(LoadAllProducts);
            frmProdInfo.ShowDialog();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            ProductBLL productBLL = new ProductBLL();
            IList<ProductInfo> products = productBLL.SearchProducts(txtSearchCondition.Text.Trim());
            dgvProducts.DataSource = products;
        }

        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProductBLL productBLL = new ProductBLL();
            if (MessageBox.Show("确定要删除该商品吗？", "系统提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    productBLL.Delete(dgvProducts.SelectedRows[0].Cells[0].Value.ToString());
                    MessageBox.Show("商品删除成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadAllProducts();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("商品删除失败！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
        
        /// <summary>
        /// added by jackie.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmProductCatergory formProdCtgy = new FrmProductCatergory();
            formProdCtgy.StartPosition = FormStartPosition.CenterParent;
            formProdCtgy.ShowDialog();
        }
        
        /// <summary>
        /// added by jackie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SpecToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Size);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }
        /// <summary>
        /// added by jackie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BrandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Brand);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }
        /// <summary>
        /// added by jackie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Color);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }
        /// <summary>
        /// added by jackie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Unit);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }
    }
}
